﻿using CIS.Client.Common.HttpRequestHelper;
using CIS.Client.Modules.Operation.Models;
using CIS.Common.Dto.Dtos.Equipments.Commands;
using CIS.Common.Enums;
using DryIoc;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows;

namespace CIS.Client.Modules.Operation.ViewModels
{
    internal class CassetteViewModel:BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly HttpRequestClient _httpRequest;
        private readonly IDialogService _dialogService;

        public CassetteViewModel(IRegionManager regionManager,HttpRequestClient httpRequest,IDialogService dialogService)
        {
            _regionManager = regionManager;
            _httpRequest = httpRequest;
            _dialogService = dialogService;
            Init();
        }

        #region 属性
        private List<PortModel>? ports;
        /// <summary>
        ///  Port 列表数据
        /// </summary>
        public List<PortModel>? Ports
        {
            get { return ports; }
            set
            {
                ports = value;
                RaisePropertyChanged(nameof(Ports));
            }
        }

        private PortModel selectePort;
        /// <summary>
        /// 当前选中的 Port
        /// </summary>
        public PortModel SelectePort
        {
            get { return selectePort; }
            set { selectePort = value; RaisePropertyChanged(nameof(SelectePort)); }
        }


        private List<string>? portControls;
        /// <summary>
        /// Port 控制命令类型 Clamp、UnClamp
        /// </summary>
        public List<string>? PortControls
        {
            get { return portControls; }
            set { portControls = value; }
        }

        private string? selectPortControlType;
        /// <summary>
        /// Port 控制命令类型 Clamp、UnClamp
        /// </summary>
        public string? SelectPortControlType
        {
            get 
            { 
                return selectPortControlType;
            }
            set
            {
                selectPortControlType = value;
                switch (selectPortControlType)
                {
                    case "Port Control Clamp":
                        PortControlCommandType= PortControlType.Clamp;
                        break;
                    case "Port Control UnClamp":
                        PortControlCommandType = PortControlType.UnClamp;
                        break;
                    case "Cassette Process Start":
                        PortControlCommandType = PortControlType.ProcessStart;
                        break;
                    case "Cassette Process Cancel":
                        PortControlCommandType = PortControlType.ProcessCancel;
                        break;
                    case "Cassette Process Abort":
                        PortControlCommandType = PortControlType.ProcessAbort;
                        break;
                    case "Cassette Process End":
                        PortControlCommandType = PortControlType.ProcessEnd;
                        break;
                }
                
            }
        }


        private PortControlType PortControlCommandType;
        #endregion

        #region 命令
        private DelegateCommand portControlCommand;
        /// <summary>
        /// 发送Port Control 按钮命令
        /// </summary>
        public DelegateCommand PortControlCommand
        {
            get 
            {
                return portControlCommand ?? new DelegateCommand(SendPortControlCommand);
            }
        }

        private DelegateCommand openCassetteMapDownLoadCommand;
        /// <summary>
        /// 打开MapDownLoad界面 按钮命令
        /// </summary>
        public DelegateCommand OpenCassetteMapDownLoadCommand
        {
            get
            {
                return openCassetteMapDownLoadCommand ?? new DelegateCommand(OpenCassetteMapDownLoadView);
            }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            Task.Run(() =>
            {
                GetPortList();
                
            });
            CreatePortControls();
        }

        /// <summary>
        /// 创建 控制命令 数据
        /// </summary>
        private void CreatePortControls()
        {
            PortControls = new List<string>
            {
                "Port Control Clamp",
                "Port Control UnClamp",
                "Cassette Process Start",
                "Cassette Process Cancel",
                "Cassette Process Abort",
                "Cassette Process End",
            };
        }

        /// <summary>
        /// 调用 API 获取 Port 数据列表
        /// </summary>
        private async void GetPortList()
        {
            //调用API
            HttpRequest request = new HttpRequest();
            request.Route = "Port/GetPorts";

            RequestResult result = await _httpRequest.SendGetAsync(request);
            Ports = JsonConvert.DeserializeObject<List<PortModel>>(Convert.ToString(result.Data));
        }

        /// <summary>
        /// 发送 Port 控制命令
        /// </summary>
        private async void SendPortControlCommand()
        {
            if(PortControlCommandType !=PortControlType.UnKonwn)
            {
                HttpRequest request = new HttpRequest();
                request.Route = "Port/SendPortControlCommand";
                var portControlCommand = new PortControlCommandDto()
                {
                    SourceId = "3AFS12-P01",
                    PortControlCommand = (Int16)PortControlCommandType
                };
                request.Parameters = JsonConvert.SerializeObject(portControlCommand);
                RequestResult result = await _httpRequest.SendPostAsync(request);
                if (result.IsSuccess)
                {
                    var reply = JsonConvert.DeserializeObject<PortControlCommandDto>(result.Data.ToString());
                    MessageBox.Show("设备回复   " + reply.PortControlCommandReturnCode.ToString());
                }
                else
                {
                    MessageBox.Show("Server Is Busy");
                }
            }
        }

        private void OpenCassetteMapDownLoadView()
        {
            _dialogService.ShowDialog("CassetteMapDownloadView");
        }
        #endregion

        #region ContentControl 相关属性
        /// <summary>
        /// TabItem 显示名称
        /// </summary>
        public string? PageTitle { get; set; } = "卡匣";

        /// <summary>
        /// 导航属性
        /// </summary>
        public string? NavUri { get; set; } = "CassetteView";

        /// <summary>
        /// 是否可关闭
        /// </summary>
        public bool IsCanClose { get; set; } = true;

        /// <summary>
        /// TabControl 关闭按钮命令
        /// </summary>
        public DelegateCommand<string>? CloseCommand
        {
            get
            {
                return new DelegateCommand<string>(CloseFunction);
            }
        }

        /// <summary>
        /// TabControl 关闭按钮执行操作
        /// </summary>
        /// <param name="navUri"></param>
        private void CloseFunction(string navUri)
        {
            var removeView = _regionManager.Regions["MainRegion"].Views.FirstOrDefault(v => v.GetType().Name == navUri);
            if (removeView != null)
            {
                _regionManager.Regions["MainRegion"].Remove(removeView);
            }
        }
        #endregion
    }
}
